package com.kgc.basicprovider.controller;

import com.kgc.basic.remote.ImportExportRemote;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.basicprovider.service.ImportExportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
public class ImportExportController implements ImportExportRemote {

    private Logger logger = LoggerFactory.getLogger(ImportExportController.class);

    @Autowired
    private ImportExportService importExportService;

    @Autowired
    private ApplicationContext applicationContext;

//    @Autowired
//    private MapperPackageConfig mapperPackageConfig;

    @Override
    public ResponseMsg importInfo(MultipartFile file, String entityClassName, String uniqueField, String flag) {
        logger.info("ImportExportController importInfo - entityClassName: {}, uniqueField: {}", uniqueField);
        try {
            Class<?> entityClass = Class.forName(entityClassName);
            ResponseMsg msg = importExportService.importEntities(file, entityClass, uniqueField, flag);
            logger.debug("ImportExportController importInfo result: {}", msg);
            return msg;
        } catch (Exception e) {
            logger.error("ImportExportController importInfo error", e);
            return new ResponseMsg("500", "导入失败：" + e.getMessage(), null);
        }
    }

    @Override
    public ResponseMsg downLoadTemplate(String entityClassName) {
        logger.info("ImportExportController downLoadTemplate - entityClassName: {}", entityClassName);
        try {
            Class<?> entityClass = Class.forName(entityClassName);
            ResponseMsg msg = importExportService.downloadTemplate(entityClass);
            logger.debug("ImportExportController downLoadTemplate result: {}", msg);
            return msg;
        } catch (ClassNotFoundException e) {
            logger.error("未找到实体类: {}", entityClassName, e);
            return new ResponseMsg("500", "无效的实体类路径：" + entityClassName, null);
        }
    }

    @Override
    public ResponseMsg exportEntities(Map<String, Object> map, String entityClassName, String orderBy) {
        logger.info("ImportExportController exportEntities - entityClassName: {}, orderBy: {}", entityClassName, orderBy);
        try {
            Class<?> entityClass = Class.forName(entityClassName);
            ResponseMsg msg = importExportService.exportEntities(map, entityClass, orderBy);
            logger.debug("ImportExportController exportEntities result: {}", msg);
            return msg;
        } catch (ClassNotFoundException e) {
            logger.error("未找到实体类或Mapper类: {}", entityClassName, e);
            return new ResponseMsg("500", "无效的实体类路径或Mapper类：" + entityClassName, null);
        } catch (Exception e) {
            logger.error("ImportExportController exportEntities error", e);
            return new ResponseMsg("500", "导出失败：" + e.getMessage(), null);
        }
    }

    @Override
    public ResponseMsg printEntitiesToPDF(Map<String, Object> map, String entityClassName, String orderBy) {
        logger.info("ImportExportController printEntitiesToPDF - entityClassName: {}, orderBy: {}", entityClassName, orderBy);
        try {
            Class<?> entityClass = Class.forName(entityClassName);
            ResponseMsg msg = importExportService.printEntitiesToPDF(map, entityClass, orderBy);
            logger.debug("ImportExportController printEntitiesToPDF result: {}", msg);
            return msg;
        } catch (ClassNotFoundException e) {
            logger.error("未找到实体类: {}", entityClassName, e);
            return new ResponseMsg("500", "无效的实体类路径：" + entityClassName, null);
        } catch (Exception e) {
            logger.error("ImportExportController printEntitiesToPDF error", e);
            return new ResponseMsg("500", "PDF 打印失败：" + e.getMessage(), null);
        }
    }


}
